package com.gomore.opple.module.main.good.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.model.SecondFilter;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by asus on 2017/5/12.
 */
public class FilerListAdapter extends BaseAdapter {

    Context mContext;
    List<SecondFilter> mListData;

    public FilerListAdapter(Context context, List<SecondFilter> listData) {
        this.mContext = context;
        this.mListData = listData;
    }

    @Override
    public int getCount() {
        return mListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mListData == null ? 0 : mListData.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_list_item, parent, false);
            viewHolder.filter_text = (TextView) convertView.findViewById(R.id.filter_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.filter_text.setText(mListData.get(position).getTypeName());
        if (mListData.get(position).isSelect()) {
            viewHolder.filter_text.setBackgroundResource(R.drawable.filter_list_selsect_bg);
            viewHolder.filter_text.setTextColor(mContext.getResources().getColor(R.color.white));
        } else {
            viewHolder.filter_text.setBackgroundResource(R.drawable.filter_list_unselect_bg);
            viewHolder.filter_text.setTextColor(mContext.getResources().getColor(R.color.blue));
        }
        viewHolder.filter_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //选中分类是Category的时候
                if (mListData.get(position).getCategory().equals(GlobalConstant.FilterCategory.CATEGORIES)) {
                    if (mListData.get(position).isSelect()) {
                        mListData.get(position).setIsSelect(false);
                    } else {
                        mListData.get(position).setIsSelect(true);
                    }
                    EventBus.getDefault().post(mListData.get(position));
                    //选中分类是非Category的时候
                } else {
                    for (int i = 0; i < mListData.size(); i++) {
                        if (i == position) {
                            if (mListData.get(i).isSelect()) {
                                mListData.get(i).setIsSelect(false);
                            } else {
                                mListData.get(i).setIsSelect(true);
                            }
                        } else {
                            mListData.get(i).setIsSelect(false);
                        }
                    }
                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
    }

    private static class ViewHolder {
        TextView filter_text;
    }
}